# Load packages ----
library(dplyr)
library(emmeans)
library(forcats)
library(ggplot2)
library(ggh4x)
library(glue)
library(gt)
library(gtsummary)
library(margins)
library(mediation)
library(multcomp)
library(modelsummary)
library(rio)

# Load and clean CES team data ----
ds <- import('data/ces-team.sav')

ds <- ds %>%
  mutate(
    treatment = factor(
      CAC344_split,
      levels = c(4, 2, 3, 1),
      labels = c('noMOD', 'trumpMOD', 'noEX', 'trumpEX')
    ),
    treatment = relevel(as.factor(treatment), ref = 'noMOD'),
    t_te = case_when(
      treatment == 'trumpMOD' | treatment == 'trumpEX' ~ 1,
      treatment == 'noMOD' | treatment == 'noEX' ~ 0
    ),
    t_si = case_when(
      treatment == 'noEX' | treatment == 'trumpEX' ~ 1,
      treatment == 'noMOD' | treatment == 'trumpMOD' ~ 0
    ),
    therm = CAC345_candidate,
    cand_voteL = ifelse(CAC346 == 8, NA, CAC346),
    cand_ideo = ifelse(CAC347 == 8 | CAC347 == 6, NA, CAC347),
    cand_enth = ifelse(CAC348 == 8, NA, abs(-CAC348)),
    pid = case_when(pid3 == 1 ~ 'dem', pid3 == 2 ~ 'rep', pid3 == 3 ~ 'ind'),
    pid = factor(pid, levels = c('rep', 'ind', 'dem')),
    pid_alt = case_when(
      pid7 < 4 ~ 'dem',
      pid7 == 4 ~ 'ind',
      pid7 > 4 & pid7 < 8 ~ 'rep'
    ),
    pid_alt = factor(pid_alt, levels = c('rep', 'ind', 'dem')),
    ideo = ifelse(ideo5 == 6, NA, ideo5),
    male = ifelse(gender4 == 1, 1, 0),
    age = 2022 - birthyr,
    white = ifelse(race == 1, 1, 0),
    black = ifelse(race == 2, 1, 0),
    hispanic = ifelse(hispanic == 1, 1, 0),
    trump16 = ifelse(presvote16post == 2, 1, 0),
    trump20 = ifelse(presvote20post == 2, 1, 0),
    ideo_alt = ifelse(CC22_340a < 8, CC22_340a, NA),
    ideo_trump = ifelse(CC22_340d < 8, CC22_340d, NA),
    vote_cand = abs(CAC346 - 6),
    vote_enth = abs(CAC348 - 4),
    vote_gen = ifelse(CAC349 %in% c(1, 3, 4), 1, 0),
    vote2020 = case_when(
      presvote20post == 1 ~ "Biden",
      presvote20post == 2 ~ "Trump",
      presvote20post == 6 ~ "Did not vote"
    ),
    vote2020 = factor(
      vote2020,
      levels = c("Trump", "Biden", "Did not vote")
    ),
    trumpFT = CAC337_1
  )

# Load and clean CES common content ----
ces <- import("data/ces-common-plus.csv")
ces <-
  ces %>%
  mutate(
    pid = case_when(
      pid3 == 1 ~ "dem",
      pid3 == 2 ~ "rep",
      pid3 == 3 ~ "ind"
    ),
    pid = factor(pid, levels = c("rep", "ind", "dem")),
    pid_alt = case_when(
      pid7 == 1 | pid7 == 2 | pid7 == 3 ~ "dem",
      pid7 == 4 ~ "ind",
      pid7 == 5 | pid7 == 6 | pid7 == 7 ~ "rep"
    ),
    pid_alt = factor(pid_alt, levels = c("rep", "ind", "dem")),
    ideo_s = ifelse(CC22_340a > 7, NA, CC22_340a),
    ideo_r = ifelse(CC22_340m > 7, NA, CC22_340m),
    ideo_d = ifelse(CC22_340l > 7, NA, CC22_340l),
    id_dist = abs(ideo_r - ideo_s) - abs(ideo_d - ideo_s),
    male = ifelse(gender4 == 1, 1, 0),
    age = 2022 - birthyr,
    white = ifelse(race == 1, 1, 0),
    black = ifelse(race == 2, 1, 0),
    hispanic = ifelse(hispanic == 1, 1, 0),
    trump16 = ifelse(presvote16post == 2, 1, 0),
    trump20 = ifelse(presvote20post == 2, 1, 0),
    voted_rep = case_when(
      CC22_412 == 2 & HouseCand2Party == "Republican" ~ 1,
      CC22_412 != 2 ~ 0
    ),
    vv = ifelse(g2022 == 6 | g2022 == -1, 0, 1),
    ideo_trump = ifelse(CC22_340d < 8, CC22_340d, NA)
  )

# Load ideological data ----
hrc22_full <- import("data/hrc22_full.csv")

# Functions ----
estFormat <- function(data, x, p) {
  lab <- sprintf(
    "%s%s",
    format(round(data$AME, 2), nsmall = 2),
    case_when(
      data$p < 0.001 ~ "***",
      data$p < 0.01 ~ "**",
      data$p < 0.05 ~ "*",
      data$p >= 0.05 ~ ""
    )
  )
  return(lab)
}
